package com.company.test5;

import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        Students[] students = new Students[3];
//      Scanner sc = new Scanner(System.in);
        students[0] = new Students("001", "赵千羽", 23);
        students[1] = new Students("002", "王若欣", 24);
        students[2] = new Students("003", "沈万三", 25);


        Students student4 = new Students("004", "王宝强", 26);

        int count = 0;
        // 计数
        for (int i = 0; i < students.length; i++) {
            // 不为null才去计数
            if (students[i] != null) {
                count++;
            }
        }

        System.out.println("下面是增加某学生");
        if(!contains(students, student4.getStudentId())) {
            if (count == students.length) {
                // 已经存满
                Students[] newStudents = createNewArr(students);
                newStudents[count] = student4;
                print(newStudents);

            } else {
                // 没有存满
                students[count] = student4;
                print(students);
            }
        } else {
            System.out.println("有学号重复的id学生编号");
        }

        System.out.println("下面是删除掉的学生编号");
        // 删除学生
        int index = getIndex(students, "001");
        if (index >= 0) {
            students[index] = null;
            print(students);
        } else {
            System.out.println("当前id不存在删除失败");
        }

        System.out.println("下面是某学生年龄+1");
        // 对某学生年龄+1
        int index2 = getIndex(students, "002");
        if (index2 >= 0) {
            students[index2].setAge(students[index2].getAge() + 1);
            print(students);
        } else {
            System.out.println("当前学生不存在");
        }


    }

    public static int getIndex(Students[] students, String id) {
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null) {
                if (students[i].getStudentId() == id) {
                    return i;
                }
            }

        }
        return -1;
    }

    public static Students[] createNewArr(Students[] students) {
        Students[] newArr = new Students[students.length + 1];

        for (int i = 0; i < students.length; i++) {
            newArr[i] = students[i];

        }
        return newArr;
    }

    public static void print(Students[] students) {
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null) {
                System.out.println(students[i].getStudentId() + " " + students[i].getName() + " " + students[i].getAge());
            }

        }
    }

    public static boolean contains(Students[] students, String id) {
        for(int i = 0; i < students.length; i++) {
            if (students[i] != null) {
                if (students[i].getStudentId() == id) {
                    return false;
                }
            }

        }
        return false;
    }
}
